﻿namespace zyn_hy_login
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CData
    {
        public SqlDataAdapter sqladapter;
        public SqlCommandBuilder sqlbuilder;
        public SqlCommand sqlcmd;
        public SqlConnection sqlconn = new SqlConnection(DBCommon.strConnectstring);
        public SqlDataReader sqlreader;

        public string creat_select_command(string[][] select_value, string table_name)
        {
            string text = "Select ";
            for (int i = 0; i < select_value[0].Length; i++)
            {
                if (i == (select_value[0].Length - 1))
                {
                    text = text + select_value[0][i];
                }
                else
                {
                    text = text + select_value[0][i] + ",";
                }
            }
            text = text + " From " + table_name;
            if (select_value[1].Length != 0)
            {
                text = text + " Where ";
                for (int j = 0; j < select_value[1].Length; j++)
                {
                    if (j == (select_value[1].Length - 1))
                    {
                        string text3 = text;
                        text = text3 + select_value[1][j] + " = '" + select_value[2][j] + "'";
                    }
                    else
                    {
                        string text4 = text;
                        text = text4 + select_value[1][j] + " = '" + select_value[2][j] + "', ";
                    }
                }
            }
            return text;
        }

        public void DataSet_fill(string sqltext, DataSet ds_table, string table_name)
        {
            this.sqladapter = new SqlDataAdapter(sqltext, this.sqlconn);
            this.sqladapter.Fill(ds_table, table_name);
        }

        public void DataSet_update(string sqltext, DataSet ds_table, string table_name)
        {
            this.sqladapter = new SqlDataAdapter(sqltext, this.sqlconn);
            this.sqlbuilder = new SqlCommandBuilder(this.sqladapter);
            this.sqladapter.Update(ds_table, table_name);
        }

        public string replace_string_yinhao(string sqlcmd)
        {
            return sqlcmd.Replace("'", "''");
        }

        public void SqlCommand_Execute(string sqltext)
        {
            this.sqlconn.Open();
            this.sqlcmd = new SqlCommand(sqltext, this.sqlconn);
            this.sqlcmd.ExecuteNonQuery();
            this.sqlconn.Close();
        }

        public SqlDataReader SqlCommand_Execute_Return(string sqltext)
        {
            this.sqlconn.Open();
            this.sqlcmd = new SqlCommand(sqltext, this.sqlconn);
            this.sqlreader = this.sqlcmd.ExecuteReader();
            return this.sqlreader;
        }

        public void SqlCommand_Insert(string[][] insert, string table_name)
        {
            this.sqlconn.Open();
            string cmdText = "Insert " + table_name + " (";
            for (int i = 0; i < insert[0].Length; i++)
            {
                if (i == (insert[0].Length - 1))
                {
                    cmdText = cmdText + insert[0][i] + ") ";
                }
                else
                {
                    cmdText = cmdText + insert[0][i] + ", ";
                }
            }
            cmdText = cmdText + "Values (";
            for (int j = 0; j < insert[0].Length; j++)
            {
                if (j == (insert[1].Length - 1))
                {
                    cmdText = cmdText + "@" + insert[0][j] + ") ";
                }
                else
                {
                    cmdText = cmdText + "@" + insert[0][j] + ", ";
                }
            }
            this.sqlcmd = new SqlCommand(cmdText, this.sqlconn);
            for (int k = 0; k < insert[0].Length; k++)
            {
                string parameterName = "@" + insert[0][k];
                this.sqlcmd.Parameters.Add(parameterName, insert[1][k].ToString());
            }
            this.sqlcmd.ExecuteNonQuery();
            this.sqlconn.Close();
        }

        public void SqlCommand_Update(string[][] update_and_select, string table_name)
        {
            this.sqlconn.Open();
            string cmdText = "Update ";
            cmdText = cmdText + table_name + " set";
            for (int i = 0; i < update_and_select[0].Length; i++)
            {
                if (i == (update_and_select[0].Length - 1))
                {
                    string text4 = cmdText;
                    cmdText = text4 + " " + update_and_select[0][i] + " = @" + update_and_select[0][i] + "_update";
                }
                else
                {
                    string text5 = cmdText;
                    cmdText = text5 + " " + update_and_select[0][i] + " = @" + update_and_select[0][i] + "_update, ";
                }
            }
            cmdText = cmdText + " where";
            for (int j = 0; j < update_and_select[2].Length; j++)
            {
                if (j == (update_and_select[2].Length - 1))
                {
                    string text6 = cmdText;
                    cmdText = text6 + " " + update_and_select[2][j] + " = @" + update_and_select[2][j] + "_select";
                }
                else
                {
                    string text7 = cmdText;
                    cmdText = text7 + " " + update_and_select[2][j] + " = @" + update_and_select[2][j] + "_select, ";
                }
            }
            this.sqlcmd = new SqlCommand(cmdText, this.sqlconn);
            for (int k = 0; k < update_and_select[0].Length; k++)
            {
                string parameterName = "@" + update_and_select[0][k] + "_update";
                this.sqlcmd.Parameters.Add(parameterName, update_and_select[1][k].ToString());
            }
            for (int m = 0; m < update_and_select[2].Length; m++)
            {
                string text3 = "@" + update_and_select[2][m] + "_select";
                this.sqlcmd.Parameters.Add(text3, update_and_select[3][m].ToString());
            }
            this.sqlcmd.ExecuteNonQuery();
            this.sqlconn.Close();
        }
    }
}

